package argo.hot100;

import java.util.HashSet;
import java.util.Set;

public class LongestConsecutive {
    public int longestConsecutiveOld(int[] nums) {
        int maxseq = 0, seq = 0;

        int min = 0, max = 0;
        for (int num : nums) {
            if (num < min) {
                min = num;
            }
            if (num > max) {
                max = num;
            }
        }
        int[] arr = new int[max - min + 1];
        for (int num : nums) {
            arr[num - min]++;
        }

        for (int i : arr) {
            if (i > 0) {
                seq++;
                if (seq > maxseq) {
                    maxseq = seq;
                }
            } else {
                seq = 0;
            }
        }
        return maxseq;

    }


    public int longestConsecutive(int[] nums) {

        int maxseq = 0;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        for (int num : set) {
            if (!set.contains(num - 1)) {
                int curseq = 1;
                while (set.contains(num + 1)) {
                    num++;
                    curseq++;
                }

                maxseq=Math.max(maxseq,curseq);
            }
        }

        return maxseq;

    }

    public static void main(String[] args) {
        System.out.println(new LongestConsecutive().longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
    }

}
